package com.woniuxy.happylife.auth.controller;

import com.woniuxy.happylife.auth.service.LoginService;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.entity.User;
import com.woniuxy.happylife.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Duration;

/**
 * @Title: LoginController
 * @ProjectName: happy-life
 * @Description: TODO
 * @Author: chowinggum
 * @Date: 2022/2/16 19:01
 */
@RestController
@RequestMapping("/auth")
public class LoginController {

    @Resource
    LoginService loginService;
    @Resource
    RedisTemplate redisTemplate;

    @RequestMapping("/login")
    public ResponseEntity login(@RequestBody User user , HttpServletResponse response){
        //前端后台管理页面登录验证
        User login = loginService.login(user.getUsername(), user.getMpassword());
        if(login==null){
            return new ResponseEntity(401,"用户名或者密码错误",null);
        }else{
            //login存在，生成token
            String token = JwtUtil.create(login.getUsername());
            login.setToken(token);
            //将token和对象存入reids中，存两个，一个30分钟，一个60分钟
            redisTemplate.opsForHash().put("token",token,login);
            redisTemplate.expire("token",Duration.ofMinutes(30));
            redisTemplate.opsForHash().put("tokencopy",token,login);
            redisTemplate.expire("tokencopy",Duration.ofMinutes(60));

            //将token设置到响应头中
            response.setHeader("token", token);
            //前后端分离项目，必须要暴露响应头，前端才能获取
            response.setHeader("Access-Control-Expose-Headers", "token");
            return new ResponseEntity(200,"login success",login);

        }

    }

    //测试方法，用来模拟注册生成用户
    @PostMapping("/enroll")
    public int enroll(String username,String password){
        return loginService.enroll(username, password);
    }

    @GetMapping("/loginout")
    public ResponseEntity<Boolean> loginout(HttpServletRequest request){
        String token = request.getHeader("Authorization");

//        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1bmFtZSI6IjEyMyIsImlzcyI6ImphdmEiLCJleHAiOjE2NDU1MTI4NTYsImlhdCI6MTY0NTUxMTA1Nn0.I__0cm17Qw1kjZ78bBw-Gb7wPaxEOkR8Li9nlNaI8vk";
        String username = JwtUtil.getUname(token);
        //删除redis中对应的以token为kay的数据
        Boolean token1 = redisTemplate.opsForHash().hasKey("token",token);
        Boolean tokencopy = redisTemplate.opsForHash().hasKey("tokencopy", token);

        if(token1||tokencopy){
            redisTemplate.opsForHash().delete("token",token);
            redisTemplate.opsForHash().delete("tokencopy",token);

        }

        Boolean rbacMenus = redisTemplate.hasKey("menu" + username);
        if(rbacMenus){
            redisTemplate.delete("menu"+username);
        }
        return new ResponseEntity<Boolean>(200, "退出登录成功", true);
    }
}
